package co.com.javeriana.aes.oms.ejb.orders.service;

import co.com.javeriana.aes.oms.common.entity.Campaign;
import co.com.javeriana.aes.oms.common.entity.Customer;
import co.com.javeriana.aes.oms.common.entity.Items;
import co.com.javeriana.aes.oms.common.entity.Orders;
import co.com.javeriana.aes.oms.ejb.orders.dao.TopFiveDAO;
import co.com.javeriana.aes.oms.ejb.orders.dao.TopOpenOrdersDAO;
import co.com.javeriana.aes.oms.ejb.orders.dao.TopProductDAO;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@Local(OrdersServiceBeanLocal.class)
@Stateless(name = "OrdersServiceBean", mappedName = "ejb/OrdersServiceBean")
public class OrdersServiceBean implements OrdersServiceBeanLocal {

    @PersistenceContext(unitName = "OMS-PU")
    private EntityManager em;

    /**
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Orders findOrderByID(int orderId) throws Exception {
        Orders order = null;
        try {
            order = em.find(Orders.class, orderId);
        } catch (Exception e) {
            throw e;
        }
        return order;
    }  
    
    
    /**
     * 
     * @param customerId
     * @return
     * @throws Exception 
     */
    @Override
    public List <Orders> findOrderByCustomerId(int customerId) throws Exception {
        
        List <Orders> order = null;
        try {
             Query data = em.createNamedQuery("Orders.findByCustomerId");
             data.setParameter("cusid", customerId);
             order = data.getResultList();                    
        } catch (Exception e) {
            throw e;
        }
        return order;
    }

    /**
     *
     * @param customerID
     * @return
     * @throws Exception
     */
    @Override
    public Customer getCustomer(int customerID) throws Exception {
        Customer customer = null;

        try {
            customer = em.find(Customer.class, customerID);
        } catch (Exception e) {
            throw e;
        }
        return customer;
    }

    /**
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Orders getOrder(int orderId) throws Exception {
        Orders order = null;

        try {
            order = em.find(Orders.class, orderId);
        } catch (Exception e) {
            throw e;
        }
        return order;
    }
    
    
    /**
     * 
     * @param order
     * @throws Exception 
     */
    @Override
    public void updateOrder(Orders order) throws Exception {
        try {
            em.merge(order);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     *
     * @param order
     * @param items
     * @return
     * @throws Exception
     */
    @Override
    public Integer createOrder(Orders order, List<Items> items) throws Exception {

        try {

            em.persist(order);
            em.flush();

            for (Items item : items) {
                item.setOrdersOrdid(order);
                em.persist(item);
            }
        } catch (Exception e) {
            throw e;
        }
        return order.getOrdid();
    }

    /**
     *
     * @return
     */
    @Override
    public List<Campaign> getCampaigns() {

        List<Campaign> c = null;

        try {
            Query data = em.createNamedQuery("Campaign.findAll");
            c = data.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }

    /**
     * 
     * @param idProduct Id Product for search topFive
     * @return List<Integer> TopFive Products order by count
     * @throws Exception 
     */
    @Override
    public List<Integer> getTopFiveProducts(int idProduct) throws Exception {
        List<Integer> c = null;
        try {
              TopFiveDAO tdao = new TopFiveDAO();
                c = tdao.getTopFiveProducts(idProduct);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
     
    /**
     * 
     * @return List<Integer> Top Products order by count
     * @throws Exception 
     */
    @Override
    public List<Integer> getTopProducts() throws Exception {
        List<Integer> c = null;
        try {
              TopProductDAO tdao = new TopProductDAO();
                c = tdao.getTopProducts();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
    
     /**
     * 
     * @return List<Integer> Top Products order by count
     * @throws Exception 
     */
    @Override
    public List<Integer> getTopOpenOrders() throws Exception {
        List<Integer> c = null;
        try {
              TopOpenOrdersDAO tdao = new TopOpenOrdersDAO();
                c = tdao.getTopOpenOrders();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }

}
